// import * as types from '@/store/mutation-type.js'
import * as CONSTANTS from "@/constants/index"
// import $axios from '@/api/index.js'
import ROUTER from '@/router/index'
// import STORE from '@/store/index'
import { API_BASEPREFIX} from "@/constants/index";
import { Toast } from 'vant'


// console.warn('API_BASEPREFIX');
// console.log(API_BASEPREFIX);
// console.log('\n');


function convertObj(data) {
  var _result = []
  for (var key in data) {
    var value = data[key]
    if (value.constructor == Array) {
      value.forEach(function(_value) {
        _result.push(key + '=' + _value)
      })
    } else {
      _result.push(key + '=' + value)
    }
  }
  return _result.join('&')
}

export default {
  /*
   * 路由跳转
   * */
  [CONSTANTS.DISPATCH_REDIRECT]: async ({ commit }, payload) => {
    const { path, query, url, isWebview, isRouter } = payload    
//判断是否为开发环境
    const isDev = process.env.NODE_ENV === CONSTANTS.VAR_NODE_ENV_DEV
    if (!isWebview && isDev) return ROUTER.push({ path, query })

    //生产环境-路由跳转
    if (isRouter) return ROUTER.push({ path, query })
    
    //生产环境-原生内新开Webview
    let params = '';
    if (isWebview && url) params = { url }
    
    let system = 'ios'
    if (/(Android)/i.test(navigator.userAgent)) system = 'android'
    if (system == 'ios') {
      window.webkit.messageHandlers.web_navigite.postMessage(params)
    } else {
      window.android.playCourse('web_navigite', JSON.stringify(params))
    }
  },

  [CONSTANTS.DISPATCH_REDIRECT_HOME]: async ({ commit }, payload) => {
    
    const { path, query } = payload
    const isDev = window.location.href.indexOf('localhost:') > -1
    if (isDev) return ROUTER.push({ path, query })


    let system
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
      system = 'ios'
    } else if (/(Android)/i.test(navigator.userAgent)) {
      system = 'android'
    }

    if (system == 'ios') {
      window.webkit.messageHandlers.web_nav_popToRoot.postMessage(null)
    } else {
      window.android.playCourse('web_nav_popToRoot', "")
    }
  },
}
